package com.czy.owner.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.czy.otherlib.wheelview.adapters.ArrayWheelAdapter;
import com.czy.otherlib.wheelview.widget.OnWheelScrollListener;
import com.czy.otherlib.wheelview.widget.WheelView;


import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;


import java.util.Calendar;
import java.util.List;

/**
 * Created by hedepu on 2017-08-23
 */

public class SelectTimeView extends LinearLayout {
    private WheelView wheelViewDay, wheelViewTime;
    private int year, day;
    private View view;
    private OnSelectData onSelectData;
    private String[] DataArray;
    private String[] TimeArray = {"上午", "下午"};
    private String[] TimeArray2 = {"下午"};
    ArrayWheelAdapter<String> timeAdapter;
    private String type = "1";
    //今天是否可选
    private boolean isSelect = true;
    private String currentTime;
    private String StorrEndtime;
    private int centerHour = 1200;

    public void setOnSelectData(OnSelectData onSelectData) {
        this.onSelectData = onSelectData;
    }

    public SelectTimeView(Context context) {
        this(context, null);
    }


    public SelectTimeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectTimeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        init();
    }

    public void init() {
        view = LayoutInflater.from(getContext()).inflate(R.layout.ui_wheelview, (ViewGroup) ((Activity) getContext()).getWindow().getDecorView(), false);

        wheelViewDay = (WheelView) view.findViewById(R.id.data_wheel);
        wheelViewTime = (WheelView) view.findViewById(R.id.time_wheel);
        addView(view);
    }

    public void setData(long time, String endtime) {
        currentTime = TimeUtils.timeStamp2Date(time + "", "HHmm");
        StorrEndtime = endtime.replace(":", "");


        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        //上一月天数
        int lastMonth = 31;

        //当年
        year = calendar.get(Calendar.YEAR);
//        //当月
//        month = calendar.get(Calendar.MONTH) + 1;
//        //当天
        day = calendar.get(Calendar.DAY_OF_MONTH);

        if (Integer.parseInt(currentTime) > Integer.parseInt(StorrEndtime)) {
            isSelect = false;
            DataArray = new String[7];
        } else {
            DataArray = new String[8];
        }


        for (int i = 0; i < 8; i++) {
            if (i == 0 && !isSelect) {
            } else {
                long selectTime = TimeUtils.getLaterDate(time, i);
                DataArray[isSelect ? i : i - 1] = TimeUtils.timeStamp2Date(selectTime + "", "yyyy年MM月dd日") + TimeUtils.getWeeky(selectTime);

            }
        }

        ArrayWheelAdapter<String> yearAdapter = new ArrayWheelAdapter<String>(getContext(), DataArray);

        yearAdapter.setTextSize(20);
        wheelViewDay.setViewAdapter(yearAdapter);
        wheelViewDay.setVisibleItems(5);
        wheelViewDay.setCurrentItem(0);
        if (isSelect && Integer.parseInt(TimeUtils.timeStamp2Date(time + "", "dd")) == day && Integer.parseInt(currentTime) > centerHour) {
            type = "2";
            timeAdapter = new ArrayWheelAdapter<String>(getContext(), TimeArray2);
        } else {
            type = "1";
            timeAdapter = new ArrayWheelAdapter<String>(getContext(), TimeArray);

        }
        timeAdapter.setTextSize(20);
        wheelViewTime.setViewAdapter(timeAdapter);
        wheelViewTime.setCurrentItem(0);
        wheelViewTime.setVisibleItems(5);
        wheelViewTime.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
            }
        });
        wheelViewDay.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {

                if (isSelect && Integer.parseInt(DataArray[wheelViewDay.getCurrentItem()].substring(8, 10)) == day && Integer.parseInt(currentTime) > centerHour) {
                    type = "2";
                    timeAdapter = new ArrayWheelAdapter<String>(getContext(), TimeArray2);
                    timeAdapter.setTextSize(20);
                    wheelViewTime.setViewAdapter(timeAdapter);
                    wheelViewTime.setCurrentItem(0);
                    wheelViewTime.setVisibleItems(5);
                } else if (isSelect) {
                    type = "1";
                    timeAdapter = new ArrayWheelAdapter<String>(getContext(), TimeArray);
                    timeAdapter.setTextSize(20);
                    wheelViewTime.setViewAdapter(timeAdapter);
                    wheelViewTime.setCurrentItem(0);
                    wheelViewTime.setVisibleItems(5);
                }
            }
        });
    }

    public String getTimeDay() {

        return DataArray[wheelViewDay.getCurrentItem()].substring(0, DataArray[wheelViewDay.getCurrentItem()].length() - 2);

    }

    public String getTimeHour() {
        if ("2".equals(type))
            return TimeArray2[0];
        return TimeArray[wheelViewTime.getCurrentItem()];

    }

    public interface OnSelectData {
        void onData(String day, String hour);
    }
}
